<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="content">
      <!-- 头部功能 -->
      <template #header>
        <div>
          <el-input class="search-input" placeholder="请输入内容" size="medium" clearable
            v-model="query" 
            @clear="reloadUserlistQuery"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="reloadUserlistQuery"></el-button>
            </template>
          </el-input>
          <el-button class="add-btn" type="primary" size="medium"
            >添加新用户</el-button
          >
        </div>
      </template>
      <!-- 正文内容 -->
      <el-table :data="users" style="width: 100%" border size="small" v-loading="loading">
        <template #empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column
          type="index"
          label="序号"
          width="60px"
        ></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态" >
          <template #default="{ row }">
            <el-switch
              v-model="row.mg_state"
              @change="updateUserSate(row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px" #default="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeUserById(row.id)"
          ></el-button>
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-setting"
          ></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination background layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5,10,15,20]"
        :page-size="pagesize"
        :total="total"
        :current-page="pagenum"
        @current-change="reloadUserlistPage"
        @size-change="reloadUserlistSize"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:false,
      total:0,
      pagesize:5,
      pagenum:1,
      query:"",
      users:[]
    };
  },
  methods: {
    reloadUserlistPage(newPageNum){
      // console.log(this.pagenum,newPageNum);
      this.pagenum = newPageNum;
      this.loadUserListPage();
    },
    reloadUserlistSize(newPageSize){
      // console.log(this.pagesize,newPageSize);
      this.pagenum = 1;  // 每页条数发生变化，表示分页数量也将发生变化，所以需要重置页面访问
      this.pagesize = newPageSize;
      this.loadUserListPage();
    },
    reloadUserlistQuery(){
      this.pagenum = 1;
      this.loadUserListPage();
    },
    async loadUserListPage() {
      try {
        this.loading = true;
        let result = await this.$api.getUserList({
          query:this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        });
        if (result == "error") return;

        this.total = result.total;
        this.pagenum = result.pagenum; // 这行代码可以定义也可以不定义，主要为保证和后端服务器结果同步
        this.users = result.users;
      } catch (error) {
        console.log("UserView-loadUserListPage-error:", error);
      } finally {
        this.loading = false;
      }
    },
    async removeUserById(id) {
      try {
        // console.log("删除用户", id);
        let result = await this.$api.deleteUser(id);
        if (result == "error") return;
        this.$message.success("删除成功");

        // 当删除的数据是最后一页的最后一条数据时，必须将 pagenum-1
        let lastFlag = this.total%this.pagesize==1;
        let lastPageFlag = Math.ceil(this.total/this.pagesize)==this.pagenum;
        if(lastFlag&&lastPageFlag&&this.pagenum!=1){
          this.pagenum = this.pagenum - 1;
        }
        this.loadUserListPage();
      } catch (error) {
        console.log("UserView-removeUserById-error:", error);
      }
    },
    async updateUserSate(user) {
      try {
        console.log(user);
        let data = await this.$api.updateUserSateById(user.id, user.mg_state);
        if (data == "error") {
          user.mg_state = !user.mg_state;
        } else {
          this.$message.success(user.username + "状态修改成功");
        }
      } catch (error) {
        console.log("UserView-updateUserSate-error:", error);
      }
    },
  },
  created() {
    this.loadUserListPage();
  },
};
</script>


<style lang="less" scoped>
.content,.el-pagination{
  margin-top: 20px;
}
.search-input {
  width: 300px;
}
.add-btn {
  margin-left: 20px;
}
</style>